//
//  MoreTableViewController.m
//  Mango
//
//  Created by lanou3g on 16/5/11.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MoreTableViewController.h"
#import "ColumAndKolModel.h"
#import "ColumAndKolTableVIewCell.h"
#import "ListTableViewController.h"

#define category_url @"http://api.app.happyjuzi.com/subscribe/category"
#define kol_url @"http://api.app.happyjuzi.com/v2.10/subscribe/kol"

@interface MoreTableViewController ()
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSString *url;//该页请求数据用到的url
@property (nonatomic, copy) NSString *currentSkinModel;//夜间模式
@end

@implementation MoreTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    [self.tableView registerNib:[UINib nibWithNibName:@"ColumAndKolTableVIewCell" bundle:nil] forCellReuseIdentifier:@"ColumAndKolTableVIewCell"];
    self.tableView.rowHeight = 80;
    
    if (self.type == ButtonTypeFinecolumns) {
        self.url = category_url;
        self.navigationItem.title = @"精选栏目";
    }else {
        self.url = kol_url;
        self.navigationItem.title = @"媒体号";
    }
    
    [self refreshData];
}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    [self updateSkinModel];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --private Method--更新皮肤模式 接到模式切换的通知后会调用此方法
-(void)updateSkinModel {
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//夜间模式
        self.tableView.backgroundColor = [UIColor blackColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    } else {//日间模式
//        self.tableView.backgroundColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:0.7];
    }
    [self.tableView reloadData];
}



- (void)refreshData {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)loadNewData {
    self.page = 1;
    [self loadDataFromServerWithUrl:self.url Page:self.page];
}

- (void)loadMoreData {
    self.page++;
    [self loadDataFromServerWithUrl:self.url Page:self.page];
}

- (void)loadDataFromServerWithUrl:(NSString *)url Page:(NSInteger)page {
    NSDictionary *paramDict = @{@"net":@"wifi",@"page":@(page),@"pf":@"ios",@"res":@"320x568",@"size":@"20",@"uid":@"3971854834692451",@"ver":@"2.11"};
    [[AFHTTPSessionManager manager]GET:url parameters:paramDict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (page == 1) {
            [self.dataSource removeAllObjects];
        }
        NSDictionary *dataDict = responseObject[@"data"];
        for (NSDictionary *dict in dataDict[@"list"]) {
            ColumAndKolModel *model = [ColumAndKolModel new];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataSource addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ColumAndKolTableVIewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ColumAndKolTableVIewCell"];
    cell.model = self.dataSource[indexPath.row];
#pragma mark --private Method--更新皮肤模式
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//日间模式
        [cell updateToNightSkinMode];
    }else {
        [cell updateToDaySkinMode];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ColumAndKolModel *model = self.dataSource[indexPath.row];
    ListTableViewController *listVC = [[ListTableViewController alloc]init];
    listVC.type = self.navigationItem.title;
    listVC.ID = model.ID;
    listVC.navTitle = model.title;
    [self.navigationController pushViewController:listVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if (self.view.window == nil && [self isViewLoaded]) {
        self.view = nil;
    }
}
@end
